package com.mylove.judas.yaya.ui.activity;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.ScrollView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.mylove.judas.yaya.R;
import com.mylove.judas.yaya.ui.activity.base.BaseActivity;

import java.io.File;
import java.net.URI;

import uk.co.senab.photoview.PhotoView;

/**
 * Created by Administrator on 2018/5/2 0002.
 */

public class BigImageActivity extends BaseActivity {

    private SubsamplingScaleImageView iv_image;

    private PhotoView photoView;

    private ProgressBar pb;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getToolbarX().hide();

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_big_image;
    }

    @Override
    public void initView() {
        super.initView();


        iv_image = (SubsamplingScaleImageView) findViewById(R.id.iv_image);

        photoView = (PhotoView) findViewById(R.id.pv_long);


        pb = (ProgressBar) findViewById(R.id.pb);

        String url = getIntent().getStringExtra("URL");

//        Glide.with(this)
//                .load(url)
//                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .placeholder(R.mipmap.ic_launcher)
//                .error(R.mipmap.ic_launcher)
//                .into(new SimpleTarget<GlideDrawable>() { //这个灰常重要，作用是让图片清晰地显示
//                    @Override
//                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
//                        photoView.setImageDrawable(resource);
//                    }
//                });


        Glide.with(this).load(url)
                .downloadOnly(new SimpleTarget<File>() {
                    @Override
                    public void onResourceReady(File resource, GlideAnimation<? super File> glideAnimation) {
                        iv_image.setImage(ImageSource.uri(Uri.fromFile(resource)));
                        pb.setVisibility(View.GONE);
                    }

                    @Override
                    public void onLoadStarted(Drawable placeholder) {
                        super.onLoadStarted(placeholder);
                        pb.setVisibility(View.VISIBLE);

                    }

                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        super.onLoadFailed(e, errorDrawable);
                        showToast(R.string.image_load_fail);
                        onBackPressed();
                    }
                });

//                .asBitmap()
//                .placeholder(R.mipmap.ic_launcher)
//                .into(new SimpleTarget<Bitmap>() {
//                    @Override
//                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
//                        iv_image.setImage(ImageSource.bitmap(resource));
//                        pb.setVisibility(View.GONE);
//                    }
//
//                    @Override
//                    public void onLoadStarted(Drawable placeholder) {
//                        super.onLoadStarted(placeholder);
//                        pb.setVisibility(View.VISIBLE);
//
//                    }
//
//                    @Override
//                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
//                        super.onLoadFailed(e, errorDrawable);
//                        showToast(R.string.image_load_fail);
//                        onBackPressed();
//                    }
//                });

    }
}
